import { BaseService } from '@app/db/base/base.service';
import { PlatformConfig } from '@app/db/common/entities/PlatformConfig';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

@Injectable()
export class PlatformConfigService extends BaseService<PlatformConfig>{
    constructor(
        @InjectRepository(PlatformConfig, 'common') protected repository:Repository<PlatformConfig>
    ) {
        super()
        this.primaryKey = 'id'
    }

    async queryConfig(feilds?:Array<keyof PlatformConfig>) {
        const data = await this.find(null, feilds, 0, 1)
        return data.length > 0 ? data[0] : null
    }

    async updateConfig(dto:{[key in keyof PlatformConfig]? : PlatformConfig[key]}) {
        const data = await this.find(null, ['id'], 0, 1)
        if(data.length == 0) {
            return this.insert(dto)
        }else {
            return this.update({id : data[0].id}, dto)
        }
    }
}
